const Redlock = require('redlock');
const redis = require('redis');


const redlock = new Redlock( redis.createClient());

// 示例用法
async function exampleUsage() {
    const resourceKey = 'exampleResource';
    const lockTTL = 10000; // 锁的过期时间，单位为毫秒

    try {
        const lock = await redlock.lock(resourceKey, lockTTL);

        // 在这里执行受锁保护的代码

        // 模拟一些处理时间
        await new Promise(resolve => setTimeout(resolve, 5000));

        // 最后释放锁
        await redlock.unlock(lock);
        console.log(`Released lock for ${resourceKey}`);
    } catch (error) {
        console.error(`Failed to acquire or release lock for ${resourceKey}`, error);
    }

}

// 运行示例
exampleUsage();
